package com.cskaoyan.mall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.product.dto.SkuInfoPageDTO;
import com.cskaoyan.mall.product.dto.SpuImageDTO;
import com.cskaoyan.mall.product.dto.SpuSaleAttrInfoDTO;
import com.cskaoyan.mall.product.model.SkuInfo;
import com.cskaoyan.mall.product.query.SkuInfoParam;
import com.cskaoyan.mall.product.service.SalesAttrService;
import com.cskaoyan.mall.product.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Macro_Ray
 * @since 2024/6/10 下午5:01
 */
@RestController
@RequestMapping("/admin/product/")
public class SkuController {

    @Autowired
    SalesAttrService salesAttrService;
    @Autowired
    SkuService skuService;

    /**
     * 查询某个SPU销售属性及属性值集合
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttrInfoDTO>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttrInfoDTO> spuSaleAttrInfoDTOList = salesAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrInfoDTOList);
    }

    /**
     * 查询某个SPU的图片列表
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImageDTO>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImageDTO> spuImageDTOList = salesAttrService.getSpuImageList(spuId);
        return Result.ok(spuImageDTOList);
    }

    /**
     * 保存SKU信息
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoParam skuInfoParam) {
        skuService.saveSkuInfo(skuInfoParam);
        return Result.ok();
    }

    /**
     * 查询所有SKU分页信息列表
     */
    @GetMapping("list/{page}/{limit}")
    public Result<SkuInfoPageDTO> getSkuPageList(@PathVariable("page") Integer page,
                                                 @PathVariable("limit") Integer limit) {
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        SkuInfoPageDTO skuInfoPageDTO = skuService.getSkuPageList(skuInfoPage);
        return Result.ok(skuInfoPageDTO);
    }

    /**
     * 根据SkuId查询商品控制上架
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 根据SkuId查询商品控制下架
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        skuService.cancelSale(skuId);
        return Result.ok();
    }
}
